package com.sgb.kjwl.view.ui.adapter.manage;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.UrlConfig;
import com.sgb.kjwl.model.entity.manage.AliVideoBean;
import com.sgb.kjwl.model.entity.manage.PromotionalAlbumEntity;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity2;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PromotionalAlbumAdapter extends BaseRecycleAdapter2<PromotionalAlbumEntity> {

    private GridLayoutManager manager;
    private RequestManager requestManager;

    private Intent intent;
    private List<String> urls;

    public PromotionalAlbumAdapter(Context context, List<PromotionalAlbumEntity> data, GridLayoutManager manager, RequestManager requestManager) {
        super(context, R.layout.item_promotional_album, data);
        this.manager = manager;
        this.requestManager = requestManager;
    }

    @Override
    public void convert(ItemViewHolder holder, PromotionalAlbumEntity entity, int position) {
        ImageView ivPhoto = holder.itemView.findViewById(R.id.iv_photo);
        ImageView iv_player = holder.itemView.findViewById(R.id.iv_player);
        if (getDatas().size() > 0) {
            PromotionalAlbumEntity albumEntity = getDatas().get(position);
            if (albumEntity.getType() == 1) {//图片
                RequestOptions options = new RequestOptions();
                options.override(400, 400);
                options.transform(new RoundedCorners(5));
                requestManager.load(entity.getUrl()).apply(options).into(ivPhoto);
                iv_player.setVisibility(View.GONE);
            } else if (albumEntity.getType() == 2){//视频
                ivPhoto.setImageResource(R.mipmap.bg_jyb_error_iamge);
                iv_player.setVisibility(View.VISIBLE);
                iv_player.setImageResource(R.mipmap.bofang);
                requestManager.load(entity.getVideoCoverUrl()).error(R.mipmap.bg_jyb_error_iamge).placeholder(R.mipmap.bg_jyb_error_iamge).
                        into(ivPhoto);
            } else {
                iv_player.setVisibility(View.VISIBLE);
                iv_player.setImageResource(R.drawable.icon_vr);
                requestManager.load(entity.getUrl()).error(R.mipmap.bg_jyb_error_iamge).placeholder(R.mipmap.bg_jyb_error_iamge).
                        into(ivPhoto);
            }
            ivPhoto.setOnClickListener(v -> {
                if (!ClickUtils.isDoubleClick()) {
                    if (albumEntity.getType() == 1) {
                        openPhotoView(context, position, albumEntity);
                    } else if (albumEntity.getType() == 2) {
                        openVideoPlayView(context, albumEntity);
                    } else {
                        WebViewX5HostActivity.start(context, albumEntity.getVrUrl());
                    }
                }
            });
        }

        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height = (int)((manager.getWidth() / manager.getSpanCount()
                - 2 * holder.itemView.getPaddingLeft() - 2 * ((ViewGroup.MarginLayoutParams) params).leftMargin)*0.68);
    }

    /**
     * 相册预览
     **/
    public void openPhotoView(Context mContext, int position, PromotionalAlbumEntity entity) {
        PhotoViewActivity2.launch(mContext, entity.getUrl(), 1, entity.getRemark(), entity.getName());
    }

    /**
     * 预览
     **/
    private void openPhotoView(int position) {
        if (intent == null) {
            intent = new Intent();
        }
        if (getDatas().size() > 0) {
            urls = new ArrayList<>();
            for (PromotionalAlbumEntity entity : getDatas()) {
                urls.add(entity.getUrl());
            }
            intent.setClass(context, PhotoViewActivity.class);
            intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) urls);
            intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
            context.startActivity(intent);
        } else {
            return;
        }
    }

    /**
     * 视频播放
     **/
    public void openVideoPlayView(Context mContext, PromotionalAlbumEntity albumEntity) {
        APIManager.getInstance().changeAPIManager(UrlConfig.NEW_BASE_URL);
        APIManager.getInstance().getUploadAPI().getVideoUrlById(albumEntity.getUrl()).enqueue(new ICallBack<BaseEntity<AliVideoBean>>() {
            @Override
            public void onSuccess(BaseEntity<AliVideoBean> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    List<VideoBean> videoBeanList = new ArrayList<>();
                    VideoBean videoBean = new VideoBean();
                    videoBean.setVideoId(albumEntity.getUrl());
                    videoBean.setCoverURL(data.getData().getPlayInfoList().get(data.getData().getPlayInfoList().size() - 1).getPlayURL());
                    videoBean.setCover(data.getData().getVideoBase().getCoverURL());
                    videoBeanList.add(videoBean);

                    PhotoViewActivity2.launch(mContext, videoBeanList, 2, albumEntity.getRemark(), albumEntity.getName());
                } else {
                    MToast.showToast(mContext, data.getMsg());
                }
            }
        });
        APIManager.getInstance().changeAPIManager(UrlConfig.BASE_URL);
    }

}
